package com.example.demo2

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import io.zhuozhuo.remotetestlib.Message

class MessageView : View {


    private var mMessage: Message? = null
    private var mSize: Size? = null
    private var mStaticLayout: StaticLayout? = null

    class Size(
        //文字大小
        val message_text_size: Float = 20f,

        //文字最大宽度
        val message_text_max_width: Int = 200,

        //图片最大宽度
        val message_image_max_width: Int = 200,

        //图片最大高度
        val message_image_max_height: Int = 400,

        //内容垂直 margin
        val message_vertical_margin: Int = 10,

        //内容横向 margin
        val message_horizontal_margin: Int = 10
    )

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)

    constructor(context: Context?, message: Message, size: Size = Size()) : super(context) {
        this.mMessage = message
        this.mSize = size
        init()
    }

    private fun init() {
        mMessage?.let {
            if (it.msgType == Message.MessageTypeText) {
                val textPaint = TextPaint()
                textPaint.color = Color.RED
                textPaint.textSize = mSize!!.message_text_size
                mStaticLayout = StaticLayout.Builder.obtain(
                    it.content,
                    0,
                    it.content.length,
                    textPaint,
                    mSize!!.message_text_max_width
                ).setAlignment(Layout.Alignment.ALIGN_NORMAL).build()
            }
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var height: Int
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightRequirement = MeasureSpec.getSize(heightMeasureSpec)
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightRequirement
        } else {
            height = mStaticLayout!!.height + paddingTop + paddingBottom
            if (heightMode == MeasureSpec.AT_MOST) {
                height = height.coerceAtMost(heightRequirement)
            }
        }
        setMeasuredDimension(width, height);
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (mMessage == null) return
        if (mMessage!!.msgType == Message.MessageTypeText) {
            mStaticLayout?.draw(canvas)
        }
    }
}